package com.hczt.xhminiapp.adminapi.modules.system.controller;

import com.hczt.xhminiapp.adminapi.modules.system.service.SysUserService;
import com.hczt.xhminiapp.adminapi.modules.system.vo.AllotRole;
import com.hczt.xhminiapp.adminapi.modules.system.vo.ChangePassword;
import com.hczt.xhminiapp.adminapi.modules.system.vo.SysUserInfo;
import com.hczt.xhminiapp.adminapi.modules.system.vo.SysUserQuery;
import com.hczt.xhminiapp.common.bean.RtnResult;
import com.hczt.xhminiapp.common.exception.BizException;
import com.hczt.xhminiapp.db.entity.SysRole;
import com.hczt.xhminiapp.db.entity.SysUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 红创-马海强
 * @date 2019-03-13 11:18
 * @description 系统用户管理
 */
@RestController
@RequestMapping("/sys/users")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 系统用户分页列表
     *
     * @param query
     * @param pageable
     * @return
     */
    @GetMapping("/page")
    public Object page(SysUserQuery query, @PageableDefault Pageable pageable) {
        Page<SysUser> list = sysUserService.findSysUsers(query, pageable);
        return RtnResult.success(list);
    }

    /**
     * 系统用户详情
     *
     * @param userId
     * @return
     */
    @GetMapping("/detail")
    public Object detail(@RequestParam String userId) {
        SysUser sysUser = sysUserService.findSysUserDetail(userId);
        return RtnResult.success(sysUser);
    }

    /**
     * 系统用户添加
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/add")
    public Object add(@RequestBody SysUser sysUser) throws BizException {
        sysUserService.save(sysUser);
        return RtnResult.success();
    }

    /**
     * 系统用户修改
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/edit")
    public Object edit(@RequestBody SysUser sysUser) throws Exception {
        sysUserService.update(sysUser);
        return RtnResult.success();
    }

    /**
     * 系统用户删除
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/remove")
    public Object remove(@RequestBody SysUser sysUser) {
        sysUserService.remove(sysUser.getUserId());
        return RtnResult.success();
    }

    /**
     * 系统用户角色
     *
     * @param userId
     * @return
     */
    @GetMapping("/roles")
    public Object roles(@RequestParam String userId) {
        List<SysRole> roles = sysUserService.findSysUserRoles(userId);
        return RtnResult.success(roles);
    }

    /**
     * 分配用户角色
     *
     * @return
     */
    @PostMapping("/allot-role")
    public Object allotRole(@RequestBody AllotRole allotRole) {
        sysUserService.allotRole(allotRole);
        return RtnResult.success();
    }

    /**
     * 修改密码
     *
     * @return
     */
    @PostMapping("/change-password")
    public Object changePassword(@RequestBody ChangePassword changePassword) throws BizException {
        sysUserService.changePassword(changePassword);
        return RtnResult.success();
    }

    /**
     * 当前用户信息
     *
     * @return
     */
    @GetMapping("/current-info")
    public Object currentInfo() {
        SysUserInfo userInfo = sysUserService.getCurrentUserInfo();
        return RtnResult.success(userInfo);
    }

    /**
     * 启用系统用户
     * @param userId
     * @return
     */
    @PostMapping("/enable/{userId}")
    public Object enable(@PathVariable String userId){
        sysUserService.enableUser(userId);
        return new RtnResult<>(null);
    }

    /**
     * 停用系统用户
     * @param userId
     * @return
     */
    @PostMapping("/disable/{userId}")
    public Object disable(@PathVariable String userId){
        sysUserService.disableUser(userId);
        return new RtnResult<>(null);
    }
}
